//
//  WCExtractUrl.m
//  WebClipper
//
//  Created by Daniel Braun on 30/03/07.
//  Copyright 2007 Daniel Braun http://page-appart.net, http://braun.daniel.free.fr All rights reserved.
//

#import "WCExtractUrl.h"


@implementation WCExtractUrl

+ (NSString *) description
{
	return @"extract and follow url";
}

- (void) dealloc {
	[urlContainString release];
	[_cachedUrl release];
	[super dealloc];
}


- (id) initWithCoder:(NSCoder *) coder
{
	self=[super initWithCoder:coder];
	if ([coder allowsKeyedCoding]) {
		findUrl=[coder decodeBoolForKey:@"findUrl"];
		urlContainString=[[coder decodeObjectForKey:@"urlContainString"]retain];
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	[super encodeWithCoder:coder];
	if ( [coder allowsKeyedCoding] ) {
		[coder encodeBool:findUrl forKey:@"findUrl"];
		[coder encodeObject:urlContainString forKey:@"urlContainString"];
	}
}
- (id) handle:(DOMHTMLElement *)el  fromUrl:(NSURL *)url
{
	if (![[[el nodeName] lowercaseString]isEqualToString:@"a"]) {
		NSLog(@"looking for element in  %@\n", [el nodeName]);
		if ([el respondsToSelector:@selector(getElementsByTagName:)]) {
			el=(DOMHTMLElement *)[[el getElementsByTagName:@"A"]item:0];
		} else return nil;
	}
	if (!el) return nil;
	[self willChangeValueForKey:@"stringURL"];
	[_cachedUrl release];
	_cachedUrl=[[NSURL URLWithString:[el getAttribute:@"href"] relativeToURL:url]retain];
	[self didChangeValueForKey:@"stringURL"];
	return _cachedUrl;
}

- (NSString *) stringUrl
{
	return [_cachedUrl absoluteString];
}
- (NSURL *) URL
{
	return _cachedUrl;
}
@end
